import { readFileSync, readdirSync, writeFileSync } from 'fs'
import { resolve, join } from 'path'

const distDir = resolve('./dist')
const distIndexFilePath = join(distDir, 'index.d.ts')

const indexDTSText = readFileSync(distIndexFilePath, 'utf-8')

const componentsDirName = 'components'
const componentsDir = join(distDir, componentsDirName)
const componentsDTSText = readdirSync(componentsDir).filter(a => a.endsWith('.vue.d.ts')).sort().map(a => {
    let componentName = a.split('.')[0]
    return `        ${componentName}: typeof import('./${componentsDirName}/${a}')['default']`
}).join('\n')
let globalDTSText = indexDTSText
    + '\n'
    + `declare module '@vue/runtime-core' {
    export interface GlobalComponents {
${componentsDTSText}
    }
}`
writeFileSync(distIndexFilePath, globalDTSText)